//
//  PlayBarView.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/13.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "PlayBarView.h"
#import "UIView+Frame.h"
#import "CFMacro.h"
#import "GX_Button.h"

@interface PlayBarView ()

@property (nonatomic, strong) GX_Button *playButton;
@property (nonatomic, strong) GX_Button *nextButton;
@property (nonatomic, strong) GX_Button *listButton;
@property (nonatomic, copy) PlayButton play;
@property (nonatomic, copy) PauseButton pause;
@property (nonatomic, copy) ListButton listOn;
@property (nonatomic, copy) ListButton listOff;

@end

@implementation PlayBarView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUp];
    }
    return self;
}
- (void)setUp {
    /**
     * slider
     */
    _slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 2)];
    [_slider setThumbImage:[UIImage imageNamed:@"btn_progress_hl"] forState:UIControlStateNormal];
    _slider.minimumTrackTintColor = [UIColor colorWithRed:0.14 green:0.08 blue:0.28 alpha:1.00];
    [self addSubview:_slider];
    /**
     * imageView
     */
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
    _imageView.backgroundColor = [UIColor orangeColor];
    _imageView.image = [UIImage imageNamed:@"cats"];
    [self addSubview:_imageView];
    /**
     * title label
     */
    _tittleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imageView.gx_width + _imageView.gx_x + 10, 10, self.gx_width - 150, self.gx_height / 3.0)];
    _tittleLabel.font = [UIFont systemFontOfSize:13];
    _tittleLabel.text = @"喵神";
    [self addSubview:_tittleLabel];
    /**
     * detail label
     */
    _detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(_tittleLabel.gx_x, _tittleLabel.gx_y + _tittleLabel.gx_height, _tittleLabel.gx_width, _tittleLabel.gx_height)];
    _detailLabel.font = [UIFont systemFontOfSize:10];
    _detailLabel.textColor = [UIColor lightGrayColor];
    _detailLabel.text = @"晚安城市 00:07 - 07:08";
    [self addSubview:_detailLabel];
    /**
     * play button
     */
    _playButton = [self buttonWithImageName:@"btn_pause_nor" action:@selector(play:)];
    [_playButton setImage:[UIImage imageNamed:@"btn_pause_hl"] forState:UIControlStateHighlighted];
    _playButton.frame = CGRectMake(self.gx_width - 30 - 20 - 30, 9, 31, 31);
    [self addSubview:_playButton];
    /**
     * list button
     */
    _listButton = [self buttonWithImageName:@"btn_showlist_nor" action:@selector(listOn:)];
    [_listButton setImage:[UIImage imageNamed:@"btn_showlist_hl"] forState:UIControlStateHighlighted];
    [_listButton setFrame:CGRectMake(self.gx_width - 30 - 10, 12, 25, 25)];
    [self addSubview:_listButton];
}
- (GX_Button *)buttonWithImageName:(NSString *)imageName action:(SEL)action {
    GX_Button *button = [GX_Button buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}
- (void)play:(PlayButton)play pause:(PauseButton)pause listOn:(ListButton)listOn listOff:(ListButton)listOff {
    self.play = play;
    self.pause = pause;
    self.listOn = listOn;
    self.listOff = listOff;
}
// 点击播放按钮后, 移除播放的点击事件, 添加暂停的点击事件
- (void)play:(GX_Button *)playButton {
    [playButton setImage:[UIImage imageNamed:@"btn_play_nor"] forState:UIControlStateNormal];
    [playButton setImage:[UIImage imageNamed:@"btn_play_hl"] forState:UIControlStateHighlighted];
    [playButton removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [playButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    self.play(playButton);
}
- (void)pause:(GX_Button *)pauseButton {
    [pauseButton setImage:[UIImage imageNamed:@"btn_pause_nor"] forState:UIControlStateNormal];
    [pauseButton setImage:[UIImage imageNamed:@"btn_pause_hl"] forState:UIControlStateHighlighted];
    [pauseButton removeTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [pauseButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(pauseButton);
}
- (void)listOn:(GX_Button *)listOn {
    [listOn removeTarget:self action:@selector(listOn:) forControlEvents:UIControlEventTouchUpInside];
    [listOn addTarget:self action:@selector(listOff:) forControlEvents:UIControlEventTouchUpInside];
    self.listOn(listOn);
}
- (void)listOff:(GX_Button *)listOff {
    [listOff removeTarget:self action:@selector(listOff:) forControlEvents:UIControlEventTouchUpInside];
    [listOff addTarget:self action:@selector(listOn:) forControlEvents:UIControlEventTouchUpInside];
    self.listOff(listOff);
}

@end
